package com.personalsystem.login.action;


import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.personalsystem.account.bean.Account;
import com.personalsystem.account.dao.AccountDao;
import com.personalsystem.login.bean.TreeEntity;
import com.personalsystem.login.service.LoginService;
import com.personalsystem.tool.BackJson;


@Controller
public class LoginAction {
	
	@Autowired
	@Qualifier("loginService")
	private LoginService loginService;
	
	@Autowired
	@Qualifier("accountDao")
	private AccountDao accountDao;
	
	@RequestMapping("/login/login.do")
	public void login(String username,String password,String checkcode,HttpServletRequest request,HttpServletResponse response){
		
		String scode=request.getSession().getAttribute("checkcode").toString();
		if(!scode.equals(checkcode)){
			BackJson.backJsonToHTML(response, "{success:false,msg:'验证码错误'}");
			return ;
		}
		Account account=accountDao.findAccount(username, password);
		if(account==null){
			BackJson.backJsonToHTML(response, "{success:false,msg:'用户名或密码错误'}");
			return ;
		}
		BackJson.backJsonToHTML(response, "{success:true,msg:'保存成功'}");
	}
	
	
	@RequestMapping("/login/loadTree.do")
	public void loadTree(HttpServletRequest request,HttpServletResponse response){
		String treeJson=request.getSession().getServletContext().getAttribute("treeJson").toString();
		BackJson.backJsonToHTML(response, treeJson);
	}
//	
//	@RequestMapping("/login/loginTree.do")
//	public void loginTree(HttpServletRequest request,HttpServletResponse response){
//		BackJson.backJsonToHTML(response, "[{id:'1',text:'123',children:[{id:'3',text:'123',leaf:true},{id:'4',text:'123',leaf:true},{id:'5',text:'123',leaf:true}]}]");
//	}
//	
//	@RequestMapping("/login/loginTable.do")
//	public void loginTable(int start,int limit,HttpServletRequest request,HttpServletResponse response){
//		System.out.println(start);
//		System.out.println(limit);
//		BackJson.backJsonToHTML(response, "{totalCount:'100',list:[{name:'xieyaxiong',sex:'男',age:'22'},{name:'xieyaxiong',sex:'男',age:'22'}]}");
//	}
//	
//	@RequestMapping("/login/loginCombox.do")
//	public void loginCombox(HttpServletRequest request,HttpServletResponse response){
//		BackJson.backJsonToHTML(response, "[{name:'xie',sex:'男',age:'22'},{name:'xie1231',sex:'男',age:'22'}]");
//		
//	}
//	
//	@RequestMapping("/login/loadTree.do")
//	public void loadTree(HttpServletRequest request,HttpServletResponse response){
//		
//		String node = request.getParameter("node");
//		System.out.println(node);
//		List<NavigationTree> treeList=loginService.loadTree(Long.parseLong(node));
//		List<Map<String, String>> mapList=new ArrayList<Map<String,String>>();
//		for(NavigationTree tree:treeList){
//			Map<String, String> m=new HashMap<String, String>();
//			m.put("id", tree.getId()+"");
//			m.put("text", tree.getNodename());
//			m.put("leaf", tree.getLeaf()+"");
//			m.put("tourl", tree.getUrl());
//			mapList.add(m);
//		}
//		JSONObject json=new JSONObject();
//		json.put("list", mapList);
//		System.out.println(json.toString());
//		BackJson.backJsonToHTML(response,json.getString("list"));
//		
//	}
//	
	

}
